package com.cellngine.client;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import static org.lwjgl.opengl.GL11.*;

import de.matthiasmann.twl.utils.PNGDecoder;
import de.matthiasmann.twl.utils.PNGDecoder.Format;

public class TextureManager
{
	private List<Texture>	textures;
	
	public TextureManager()
	{
		textures = new ArrayList<Texture>();
	}
	
	public void loadTexture(final String path, final String name, final int FILE_FORMAT)
	{
		
		if (name.isEmpty())
		{
			System.out.println("A texture name must be supplied.");
			return;
		}
		
		if (FILE_FORMAT != FileFormat.FORMAT_PNG)
		{
			System.out.println("Currently only PNG format is supported.");
			return;
		}
		else
		{
			try
			{
				final InputStream in = new FileInputStream(path);
				final PNGDecoder decoder = new PNGDecoder(in);
				final int width = decoder.getWidth();
				final int height = decoder.getHeight();
				final ByteBuffer data = ByteBuffer.allocateDirect(4 * width * height);
				decoder.decode(data, 4 * width, Format.RGBA);
				data.flip();
				
				final int glid = glGenTextures();
				glBindTexture(GL_TEXTURE_2D, glid);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
						GL_UNSIGNED_BYTE, data);
				
				final Texture t = new Texture(name, path, width, height, FILE_FORMAT, data, glid);
				textures.add(t);
				System.out.println("Texture " + name + " (" + path + ") loaded to index "
						+ textures.indexOf(t) + " GLID " + glid);
			}
			catch (final FileNotFoundException e)
			{
				e.printStackTrace();
				return;
			}
			catch (final IOException e)
			{
				e.printStackTrace();
				return;
			}
		}
		
	}
	
	public Texture getTexture(final String name)
	{
		for (int i = 0; i < textures.size(); i++)
		{
			final Texture tmp = textures.get(i);
			if (name.compareToIgnoreCase(tmp.getTextureName()) == 0)
			{
				return tmp;
			}
		}
		System.out.println("Warning: Could not get texture " + name);
		return null;
	}
	
	public void deleteTexture(final int index)
	{
		if (index > textures.size())
		{
			System.out.println("Warning: Given index for texture deletion is out of range.");
			return;
		}
		final Texture tmp = textures.get(index);
		textures.remove(index);
		System.out.println("Texture " + tmp.getTextureName() + "(" + index + ") has been deleted.");
	}
	
	public void deleteTexture(final String name)
	{
		for (int i = 0; i < textures.size(); i++)
		{
			final Texture tmp = textures.get(i);
			if (name.compareToIgnoreCase(tmp.getTextureName()) == 0)
			{
				textures.remove(i);
				System.out.println("Texture " + tmp.getTextureName() + "(" + i
						+ ") has been deleted.");
				return;
			}
		}
	}
	
	public int getLoadedTextureCount()
	{
		return textures.size();
	}
	
}
